<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="nav">
    <div class="left-nav">
        <div id="side-nav">
            <ul id='nav'></ul>
        </div>
    </div>
    <!-- <div class="x-slide_left"></div> -->
    <!-- 左侧菜单结束 -->
    <script type="text/javascript">
        function buildD(data, isRoot) {
            var result = "";
            if (!isRoot) {
                result = "<ul class='sub-menu'>"
            }
            data.forEach(function (item, idx) {
                result += "<li>";
                if (item.resType == "D") {
                    result += ['<a href="javascript:;">'
                        , '<i class="left-nav-li ', item.icon, '" lay-tips="', item.resName, '"></i>'
                        , '<cite>', item.resName, '</cite>'
                        , '<i class="iconfont nav_right ">&#xe6a7;</i></a>'].join('');
                } else if (item.resType == "M") {
                    result += ['<a onclick="xadmin.add_tab(\'', item.resName, '\',\'', item.resPath, '\')">'
                        , '<i class="  left-nav-li ', item.icon, '" lay-tips="', item.resName, '"></i>'
                        , '<cite>', item.resName, '</cite></a>'].join('');
                }
                if (item.children) {
                    result += buildD(item.children, false);
                }
                result += "</li>";
            })
            if (!isRoot) {
                result += "</ul>";
            }
            return result;
        }

    </script>
    <script>
        layui.use(['laytpl', 'jquery'], function () {
            var $ = layui.jquery,
                view = document.getElementById('nav');
            $.get('/system/menu.json', {}, function (res, status) {
                var d = res.data.menus;
                var level = res.data.level;
                var tMap = {};
                //构建字典
                for (var i = 0; i < d.length; i++) {
                    var item = d[i];
                    var tt = tMap[item.parentId] || {child: []};
                    tt.child[tt.child.length] = {pos: i, resId: item.resId, o: item.order};
                    tMap[item.parentId] = tt;
                }

                //构建2级tree（n)
                function list2Tree(data, dict, key, level, idStr) {
                    var child = [];
                    level--;
                    var tmp = dict[key];
                    if (tmp && tmp['child']) {
                        tmp = tmp.child;
                        for (var j = 0; j < tmp.length; j++) {
                            var c = data[tmp[j].pos]
                            c['id'] = (idStr == "" ? "" : (idStr + "_")) + j;
                            if (level > 0) {
                                c['children'] = list2Tree(data, dict, c.resId, level, c['id']);
                            }
                            child[child.length] = c;
                        }
                    }
                    return child;
                }

                var root = list2Tree(d, tMap, 0, level, "");
                view.innerHTML = buildD(root, true);
                console.log('add html')
            })
        })
    </script>
</th:block>
</html>